package com.huizhonglm.diap.core.image;

import android.widget.ImageView;

import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.api.API;

import org.xutils.image.ImageOptions;
import org.xutils.x;

/**
 * XUtils中的图片加载器
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2014-11-04 13:53
 */
public class ThumbXutils {
	private static ThumbXutils mInstance;

    /**
	 * 获取单例
	 *
	 * @return Thumb
	 */
	public static ThumbXutils getInstance() {
		if (mInstance == null) {
			synchronized (ThumbXutils.class) {
				if (mInstance == null) {
					mInstance = new ThumbXutils();
				}
			}
		}
		return mInstance;
	}

	private ThumbXutils() {
	}

	public  void display(ImageView iv, String url, int defDrawableId) {
        if (!EmptyUtil.isEmpty(url)) {
			x.image().bind(iv, getFullURL(url), getImageOptions(defDrawableId));
        }else{
        	iv.setImageResource(defDrawableId);
        }
	}

	public ImageOptions getImageOptions(int defDrawableId){
		return new ImageOptions.Builder()
				// 如果ImageView的大小不是定义为wrap_content, 不要crop.
				.setCrop(true) // 很多时候设置了合适的scaleType也不需要它.
				// 加载中或错误图片的ScaleType
				//.setPlaceholderScaleType(ImageView.ScaleType.MATRIX)
				.setImageScaleType(ImageView.ScaleType.CENTER_CROP)
				.setLoadingDrawableId(defDrawableId)
				.setFailureDrawableId(defDrawableId)
				.build();
	}

    private String getFullURL(String url){
        if (!url.startsWith("http://") &&
				!url.startsWith("HTTP://") &&
				!url.startsWith("file://") &&
				!url.startsWith("FILE://") &&
				!url.startsWith("content://") &&
				!url.startsWith("CONTENT://")) {

        	return API.HOST + "/" + url;
        }
        return url;
    }
}
